* Table 8 
qui{
* First panel: Average expenses
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
replace experiment=0 if experiment==16
append using "Counterfactuals Stata Files/Counterfactual14.dta"
keep if age<65

*generate table as follows:
*total MC*.6 / person
*total MC*6 if treated / person 
* paid by OOP/person 
* paid by ESHI
* paid by Public 
* Unpaid


* OOP paid by individuals
gen col3= OOP if tr_pay_alt==1 
replace col3=OOP if type2==1 & govt_transfer==0  
replace col3= OOP-govt_transfer if govt_transfer<OOP & govt_transfer>0 & tr_pay_alt==2

* What ESHI pays
gen col4= MC*.6 - OOP if I_treat==1 & ESHI==1 


* Public: Medicaid and DI (not the Universal Health insurance)
gen col5=OOP  if tr_pay_alt==2 
* Medicaid only pays some of OOP when the person can afford the rest
replace col5 =  govt_transfer if govt_transfer<OOP &  tr_pay_alt==2

* Unpaid bills
gen col6 =OOP if tr_pay_alt==3 


* Costs if all treated
gen col7=OOP if tr_pay_alt==4 & experiment==0 & ESHI==0 
replace col7=MC_orig*.6 if tr_pay_alt==4 & experiment==0 & ESHI==1  
replace col7=MC_orig*.6  if tr_pay_alt==4 & experiment==14 

* Universal health insurance 
gen col8=MC_orig *.6 - OOP if ((I_treat==1 & ESHI==0) | (type2==1 & ESHI==0 ) ) & experiment==14
gen col9=MC_orig *.6 - OOP if (I_treat==1 & ESHI==0)  & experiment==14

gen count=1

* Calculate statistics for all, for benchmark and UHI experiment
collapse (sum) col*  count, by(experiment)
replace col3=col3/count
replace col4=col4/count
replace col5=col5/count
replace col6=col6/count
replace col7=col7/count
replace col8=col8/count
replace col9=col9/count
drop count


label var col3 "OOP (Self)"
label var col4 "ESHI"
label var col5 "Medicaid"
label var col6 "Unpaid"
label var col7 "Untreated"
label var col8 "Public Insurance"
label var col9 "Public Insurance - No Preventive"

gen total= col3 + col4 + col5 + col6 if experiment==0
replace total= col3 + col4 + col5 + col6 + col8 if experiment==14
label var total "Total if treated"

table  (experiment) ,  statistic(mean col3 col4 col5 col6 col8 col9 total col7)  nototals nformat(%5.0f)  

collapse (mean) col3 col4 col5 col6 col8 col9 total col7, by (experiment)
sort experiment 
drop col9 
order experiment  col8  col3 col4 col5 col6  total  col7

* Open data and copy paste in Table 8. All columns and rows are in order

sort experiment
save "Temp\Table_UHI.dta", replace	
}

* Second panel: Health outcomes
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
replace experiment=0 if experiment==16
append using "Counterfactuals Stata Files/Counterfactual14.dta"

bysort experiment ID: egen max_age=max(age)
replace max_age=max_age+1 // die at the beginning of next year
replace max_age=. if age>25 // we want one obs per person

keep if age<65

gen I_shock=0 
replace I_shock=1 if type2!=1 

gen I_Good=0 
replace I_Good=1 if H==3

* these are the numbers in panel 2
table experiment, stat(mean  I_treat I_shock I_Good max_age)

collapse (mean) I_treat I_shock I_Good max_age , by (experiment)

* OPEN DATA 
* THESE ARE ALL THE STATISTICT FOR PANEL 2 OF TABLE 8. (need to transpose, but they are in order)

sort experiment
merge experiment using "Temp\Table_UHI.dta"	
drop _merge
sort experiment
save "Temp\Table_UHI.dta", replace	
}

* Third panel: Labor Market outcomes
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
replace experiment=0 if experiment==16
append using "Counterfactuals Stata Files/Counterfactual14.dta"

bysort experiment ID: egen max_age=max(age)
replace max_age=max_age+1 // die at the beginning of next year
replace max_age=. if age>25 // we want one obs per person

rename education college_alt
sort age college_alt
merge age college_alt using "${mystart}\CPS\CPS Data Files\Intermediate Data Files/Equiv.dta"

rename college_alt education
replace equiv= 1.5 if age>64
replace equiv= 1.5 + (equiv -2.0 )*0.3 if age<65

gen family_consumption = consumption
replace consumption = consumption/ equiv if married==1

gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  
gen discounted_family_consumption = family_consumption*discounting_factor
bysort experiment ID: egen family_consumption_PV = total(discounted_family_consumption)
replace family_consumption_PV=. if age!=25
replace family_consumption_PV=family_consumption_PV/1000


gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen earnings_PV = total(discounted_earnings)
replace earnings_PV=. if age!=25
replace earnings_PV=earnings_PV/1000

* FT yrs of work
bysort experiment ID: egen yrs_work=max(human_capital_prime) 
replace  yrs_work=. if age!=25 // want only 1 obs per ID

* these are the numbers in panel 3
table experiment if age<65, stat(mean wage_offer employed_yn yrs_work family_consumption_PV earnings_PV TR) stat(cv family_consumption_PV earnings_PV)

* construct table for convenience
keep if age<65
collapse (mean) wage_offer employed_yn yrs_work family_consumption_PV earnings_PV TR (sd) CV_cons=family_consumption_PV CV_earn=earnings_PV , by (experiment)

replace CV_cons =CV_cons/family_consumption_PV // calculate coeff of variation for consumption
replace CV_earn =CV_earn/earnings_PV // calculate coeff of variation for earnings

order experiment wage_offer employed_yn yrs_work earnings_PV CV_earn family_consumption_PV CV_cons  TR

* OPEN DATA 
* THESE ARE ALL THE STATISTICT FOR PANEL 3 OF TABLE 8. (need to transpose, but they are in order)

sort experiment
merge experiment using "Temp\Table_UHI.dta"	
drop _merge
sort experiment
save "Temp\Table_UHI.dta", replace	
}

* Fourth panel: Gov revenue and expenditures per working age male
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
replace experiment=0 if experiment==16
append using "Counterfactuals Stata Files/Counterfactual14.dta"

keep if age<65

* revenue from UHI premiums
gen UHI_premium=0
replace UHI_premium = 810  if age<65 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) 

gen expenditures = Expenditures_pay if (I_treat==1 & I_pay==1) | type2==1 
replace expenditures = Expenditures_not_pay if I_pay==0
sum expenditures
* expenditures = OOP + tax paid + premiums
* first subtract premiums
*has ESHI
gen Income_tax= expenditures
replace Income_tax= expenditures - 810 if age<=64 &  (offer_accepted==3 | offer_accepted==5) &  Spouse_status!=3 // has eshi and spouse not covered or no spouse. 
replace Income_tax= expenditures - 2071 if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==3
* experiment
replace Income_tax= expenditures - 810 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status!=3
replace Income_tax= expenditures - 810*2 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status==3
* now subtract the spouse's OOP
replace Income_tax= Income_tax-OOP_spouse if married==1
* now subtract own OOP - only for those who treat and pay. 
replace Income_tax= Income_tax - OOP if type2==1 | (I_treat==1 & I_pay==1) //these always pay

gen Cons_tax = consumption * .057
gen Total_tax = Income_tax + Cons_tax

replace spouse_income=0 if spouse_income==.
replace I_treat=1 if type2==1
replace I_pay=1 if type2==1 & ESHI==1 
replace I_pay=1 if type2==1 & ESHI==0 & experiment==14 

* In the benchmark, we need to worry about ESHI and ESHI premiums
* add the ESHI premium to expenditures = 810
* ESHI premium for couples is 2071
gen gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse - 1.045*assets - earnings - spouse_income if age<=64 & experiment==0 & (offer_accepted!=3 & offer_accepted!=5) // NO INSURANCE, BENCHMARK

* for those with ESHI
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + 810  - 1.045*assets - earnings if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==0 
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse +  810  - 1.045*assets - earnings - spouse_income if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==2
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse + 2071 - 1.045*assets - earnings - spouse_income  if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==1 & Spouse_status==3

* UNINSURED IN EXPERIMENT
* experiment with UHI - now have to include total expenditures rather than OOP
* NOTE WE DON'T WANT TO SUBTRACT THE UHI PREMIUMS BECAUSE WE WANT THESE PREMIUMS TO COUNT AS PART OF THE GOV REVENUE
* no ESHI
* singles
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat  - 1.045*assets - earnings if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==0
* married
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse  - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==2  // spouse does not work
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse + 810 - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==3  // spouse works


gen Unpaid_bills=0
replace Unpaid_bills=OOP if tr_pay_alt==3 & age<65

gen UHI=0
replace UHI=MC_orig *.6 - OOP if ((I_treat==1 & ESHI==0) | (type2==1 & ESHI==0 ) ) & experiment==14 & age<65


collapse (mean) Total_tax UHI_premium UHI govt_transfer  Unpaid_bills gov_expenditure, by(experiment)
* OPEN DATA 
* THESE ARE THE NUMBERS IN THE BOTTOM PANEL OF THE TABLE (transposed)

sort experiment
rename Total_tax Total_tax1
rename govt_transfer govt_transfer1
rename Unpaid_bills Unpaid_bills1
rename gov_expenditure gov_expenditure1
merge experiment using "Temp\Table_UHI.dta"
drop _merge	
sort experiment
save "Temp\Table_UHI.dta", replace	
}


* Fifth panel: Gov revenue and expenditures per HH
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
replace experiment=0 if experiment==16
append using "Counterfactuals Stata Files/Counterfactual14.dta"

* how much revenue from UHI premiums
gen UHI_premium=0
replace UHI_premium = 810  if age<65 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5)

gen expenditures = Expenditures_pay if (I_treat==1 & I_pay==1) | type2==1 
replace expenditures = Expenditures_not_pay if I_pay==0
sum expenditures
* expenditures = OOP + tax paid + premiums
* first subtract premiums
*has ESHI: benchmark and experiment the same
gen Income_tax= expenditures
replace Income_tax= expenditures - 810 if age<=64 &  (offer_accepted==3 | offer_accepted==5) &  Spouse_status!=3 // has eshi and spouse not covered or no spouse. 
replace Income_tax= expenditures - 2071 if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==3
* experiment
replace Income_tax= expenditures - 810 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status!=3
replace Income_tax= expenditures - 810*2 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status==3
* now subtract the spouse's OOP
replace Income_tax= Income_tax-OOP_spouse if married==1
* now subtract own OOP - only for those who treat and pay. 
replace Income_tax= Income_tax - OOP if type2==1 | (I_treat==1 & I_pay==1) //these always pay

gen Cons_tax = consumption * .057
gen Total_tax = Income_tax + Cons_tax

* now let's fix the 65+
replace expenditures=0 if age>64
replace Income_tax=0 if age>64 
replace Cons_tax=0 if age>64
replace Total_tax=0 if age>64

replace expenditures = Expenditures_pay if age>64
gen Mcare_premium = 854  if age>64
replace Mcare_premium = Mcare_premium + 854 if age>64 & married==1 
replace Mcare_premium =0 if age<65

replace Income_tax=expenditures - Mcare_premium - OOP if age>64 & married==0
replace Income_tax=expenditures - Mcare_premium - OOP - OOP_spouse  if age>64 & married==1

replace Cons_tax = consumption * .057 if age>64
replace Total_tax = Income_tax + Cons_tax if age>64

replace SS_income =0 if age<65
gen total_SS_income = SS_income + spouse_income if age>64 & married==1
replace total_SS_income = SS_income if age>64 & married==0
replace total_SS_income =0 if age<65

gen Mcare_bills= 0 if age<65
replace Mcare_bills=OOP if age>64 & married==0
replace Mcare_bills=OOP + OOP_spouse if age>64 & married==1

* USE BUDGET CONSTRAINT TO INFER TOTAL GOVT EXPENDITURES FOR EACH PERSON. THIS WILL BE THE SUM OF EVERYTHING
* 65+ PEOPLE
* A' + C -RA + ME = SS - MEDICARE_PREM - INCOME_TAX - C(TAX) + ME (1-MEDICARE_COPAY) + TR
* EVERYTHING ON THE RIGHT HAND SIDE IS GOVT TRANSFERS AND REVENUE FROM THIS THIS PERSON
gen gov_expenditure= assets_prime + consumption + OOP*2 + OOP_spouse*2 - 1.045*assets if age>=65 // cons is after tax. OOP*2 is the total medical expendiure of the old. Medicare pays half by assumption. 

replace spouse_income=0 if spouse_income==.
replace I_treat=1 if type2==1
replace I_pay=1 if type2==1 & ESHI==1 
replace I_pay=1 if type2==1 & ESHI==0 & experiment==14 

* for those <65. 
* In the benchmark, we need to worry about ESHI and ESHI premiums
* add the ESHI premium to expenditures = 810
* ESHI premium for couples is 2071
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse - 1.045*assets - earnings - spouse_income if age<=64 & experiment==0 & (offer_accepted!=3 & offer_accepted!=5) // NO INSURANCE, BENCHMARK
* the following 3 lines are for those with ESHI, which are the same in benchmark and UHI experiment, so we run them for both. If anyone is also on Medicaid, that's OK because Medicaid would only pay the OOP part after ESHI paid.
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + 810  - 1.045*assets - earnings if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==0 
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse +  810  - 1.045*assets - earnings - spouse_income if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==2
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse + 2071 - 1.045*assets - earnings - spouse_income  if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==1 & Spouse_status==3

* UNINSURED IN EXPERIMENT
* experiment with UHI - now have to include total expenditures rather than OOP
* NOTE WE DON'T WANT TO SUBTRACT THE UHI PREMIUMS BECAUSE WE WANT THESE PREMIUMS TO COUNT AS PART OF THE GOV REVENUE
* no ESHI
* singles
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat  - 1.045*assets - earnings if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==0
* married
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse  - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==2  // spouse does not work
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse + 810 - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==3  // spouse works



* GENERATING THINGS FOR TABLE IN PAPER
* BOTTOM PANEL
* TAX REVENUE ; SS+medicare - medicare premiums 
gen SS_Medicare =Mcare_premium -Mcare_bills-total_SS_income


gen Unpaid_bills=0
replace Unpaid_bills=OOP if tr_pay_alt==3 & age<65
* in the benchmark, for those with ESHI, the unpaid part is only the OOP (ESHI always pays the rest). for those without ESHI, OOP is set to .6*MC. 
* in the experiment, it's the same for those with ESHI. If no ESHI, the unpaid part is just the OOP of the person. the rest gets paid by UHI

* UHI
gen UHI=0
replace UHI=MC_orig *.6 - OOP if ((I_treat==1 & ESHI==0) | (type2==1 & ESHI==0 ) ) & experiment==14 & age<65
gen UHI_total= UHI- UHI_premium

collapse (mean) Total_tax SS_Medicare govt_transfer UHI_total Unpaid_bills gov_expenditure, by(experiment)
* OPEN DATA 
* THESE ARE THE NUMBERS IN THE BOTTOM PANEL OF THE TABLE (transposed)
sort experiment
merge experiment using "Temp\Table_UHI.dta"	
drop _merge
sort experiment
save "Temp\Table_UHI.dta", replace	
}

clear
use "Temp\Table_UHI.dta"

order experiment col8  col3 col4 col5 col6  total  col7 I_treat I_shock I_Good max_age  wage_offer employed_yn yrs_work earnings_PV CV_earn family_consumption_PV CV_cons  TR Total_tax1 UHI_premium UHI govt_transfer1  Unpaid_bills1 gov_expenditure1  Total_tax SS_Medicare govt_transfer UHI_total Unpaid_bills gov_expenditure

xpose, clear
drop if v1==0 & v2==14
rename v1 Benchmark 
rename v2 Public_HI

* OPEN DATA -- COPY PASTE INTO TABLE 8 - NUMBERS IN COLUMNS ARE IN ORDER (ALL PANELS COMBINED)
}



* Table 132 - same as table 8 in paper, but HS or Less only 
qui{
* First panel: Average expenses
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
replace experiment=0 if experiment==16
append using "Counterfactuals Stata Files/Counterfactual14.dta"
keep if age<65
keep if education==1
*generate table as follows:
*total MC*.6 / person
*total MC*6 if treated / person 
* paid by OOP/person 
* paid by ESHI
* paid by Public 
* Unpaid


* OOP paid by individuals
gen col3= OOP if tr_pay_alt==1 
replace col3=OOP if type2==1 & govt_transfer==0  
replace col3= OOP-govt_transfer if govt_transfer<OOP & govt_transfer>0 & tr_pay_alt==2

* What ESHI pays
gen col4= MC*.6 - OOP if I_treat==1 & ESHI==1 


* Public: Medicaid and DI (not the Universal Health insurance)
gen col5=OOP  if tr_pay_alt==2 
* Medicaid only pays some of OOP when the person can afford the rest
replace col5 =  govt_transfer if govt_transfer<OOP &  tr_pay_alt==2

* Unpaid bills
gen col6 =OOP if tr_pay_alt==3 


* Costs if all treated
gen col7=OOP if tr_pay_alt==4 & experiment==0 & ESHI==0 
replace col7=MC_orig*.6 if tr_pay_alt==4 & experiment==0 & ESHI==1  
replace col7=MC_orig*.6  if tr_pay_alt==4 & experiment==14 

* Universal health insurance 
gen col8=MC_orig *.6 - OOP if ((I_treat==1 & ESHI==0) | (type2==1 & ESHI==0 ) ) & experiment==14
gen col9=MC_orig *.6 - OOP if (I_treat==1 & ESHI==0)  & experiment==14

gen count=1

* Calculate statistics for all, for benchmark and UHI experiment
collapse (sum) col*  count, by(experiment)
replace col3=col3/count
replace col4=col4/count
replace col5=col5/count
replace col6=col6/count
replace col7=col7/count
replace col8=col8/count
replace col9=col9/count
drop count


label var col3 "OOP (Self)"
label var col4 "ESHI"
label var col5 "Medicaid"
label var col6 "Unpaid"
label var col7 "Untreated"
label var col8 "Public Insurance"
label var col9 "Public Insurance - No Preventive"

gen total= col3 + col4 + col5 + col6 if experiment==0
replace total= col3 + col4 + col5 + col6 + col8 if experiment==14
label var total "Total if treated"

table  (experiment) ,  statistic(mean col3 col4 col5 col6 col8 col9 total col7)  nototals nformat(%5.0f)  

collapse (mean) col3 col4 col5 col6 col8 col9 total col7, by (experiment)
sort experiment 
drop col9 
order experiment  col8  col3 col4 col5 col6  total  col7

* Open data and copy paste in Table 8. All columns and rows are in order

sort experiment
save "Temp\Table_UHI.dta", replace	
}

* Second panel: Health outcomes
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
replace experiment=0 if experiment==16
append using "Counterfactuals Stata Files/Counterfactual14.dta"

bysort experiment ID: egen max_age=max(age)
replace max_age=max_age+1 // die at the beginning of next year
replace max_age=. if age>25 // we want one obs per person

keep if age<65
keep if education==1

gen I_shock=0 
replace I_shock=1 if type2!=1 

gen I_Good=0 
replace I_Good=1 if H==3

* these are the numbers in panel 2
table experiment, stat(mean  I_treat I_shock I_Good max_age)

collapse (mean) I_treat I_shock I_Good max_age , by (experiment)

* OPEN DATA 
* THESE ARE ALL THE STATISTICT FOR PANEL 2 OF TABLE 8. (need to transpose, but they are in order)

sort experiment
merge experiment using "Temp\Table_UHI.dta"	
drop _merge
sort experiment
save "Temp\Table_UHI.dta", replace	
}

* Third panel: Labor Market outcomes
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
replace experiment=0 if experiment==16
append using "Counterfactuals Stata Files/Counterfactual14.dta"
keep if education==1

bysort experiment ID: egen max_age=max(age)
replace max_age=max_age+1 // die at the beginning of next year
replace max_age=. if age>25 // we want one obs per person

rename education college_alt
sort age college_alt
merge age college_alt using "${mystart}\CPS\CPS Data Files\Intermediate Data Files/Equiv.dta"

rename college_alt education
replace equiv= 1.5 if age>64
replace equiv= 1.5 + (equiv -2.0 )*0.3 if age<65

gen family_consumption = consumption
replace consumption = consumption/ equiv if married==1

gen time_period = age-25
generate discounting_factor=0.956937799^(time_period)  
gen discounted_family_consumption = family_consumption*discounting_factor
bysort experiment ID: egen family_consumption_PV = total(discounted_family_consumption)
replace family_consumption_PV=. if age!=25
replace family_consumption_PV=family_consumption_PV/1000


gen discounted_earnings=earnings*discounting_factor
bysort experiment ID: egen earnings_PV = total(discounted_earnings)
replace earnings_PV=. if age!=25
replace earnings_PV=earnings_PV/1000

* FT yrs of work
bysort experiment ID: egen yrs_work=max(human_capital_prime) 
replace  yrs_work=. if age!=25 // want only 1 obs per ID

* these are the numbers in panel 3
table experiment if age<65, stat(mean wage_offer employed_yn yrs_work family_consumption_PV earnings_PV TR) stat(cv family_consumption_PV earnings_PV)

* construct table for convenience
keep if age<65
collapse (mean) wage_offer employed_yn yrs_work family_consumption_PV earnings_PV TR (sd) CV_cons=family_consumption_PV CV_earn=earnings_PV , by (experiment)

replace CV_cons =CV_cons/family_consumption_PV // calculate coeff of variation for consumption
replace CV_earn =CV_earn/earnings_PV // calculate coeff of variation for earnings

order experiment wage_offer employed_yn yrs_work earnings_PV CV_earn family_consumption_PV CV_cons  TR

* OPEN DATA 
* THESE ARE ALL THE STATISTICT FOR PANEL 3 OF TABLE 8. (need to transpose, but they are in order)

sort experiment
merge experiment using "Temp\Table_UHI.dta"	
drop _merge
sort experiment
save "Temp\Table_UHI.dta", replace	
}

* Fourth panel: Gov revenue and expenditures per working age male
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
replace experiment=0 if experiment==16
append using "Counterfactuals Stata Files/Counterfactual14.dta"
keep if education==1
keep if age<65

* revenue from UHI premiums
gen UHI_premium=0
replace UHI_premium = 810  if age<65 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) 

gen expenditures = Expenditures_pay if (I_treat==1 & I_pay==1) | type2==1 
replace expenditures = Expenditures_not_pay if I_pay==0
sum expenditures
* expenditures = OOP + tax paid + premiums
* first subtract premiums
*has ESHI
gen Income_tax= expenditures
replace Income_tax= expenditures - 810 if age<=64 &  (offer_accepted==3 | offer_accepted==5) &  Spouse_status!=3 // has eshi and spouse not covered or no spouse. 
replace Income_tax= expenditures - 2071 if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==3
* experiment
replace Income_tax= expenditures - 810 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status!=3
replace Income_tax= expenditures - 810*2 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status==3
* now subtract the spouse's OOP
replace Income_tax= Income_tax-OOP_spouse if married==1
* now subtract own OOP - only for those who treat and pay. 
replace Income_tax= Income_tax - OOP if type2==1 | (I_treat==1 & I_pay==1) //these always pay

gen Cons_tax = consumption * .057
gen Total_tax = Income_tax + Cons_tax

replace spouse_income=0 if spouse_income==.
replace I_treat=1 if type2==1
replace I_pay=1 if type2==1 & ESHI==1 
replace I_pay=1 if type2==1 & ESHI==0 & experiment==14 

* In the benchmark, we need to worry about ESHI and ESHI premiums
* add the ESHI premium to expenditures = 810
* ESHI premium for couples is 2071
gen gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse - 1.045*assets - earnings - spouse_income if age<=64 & experiment==0 & (offer_accepted!=3 & offer_accepted!=5) // NO INSURANCE, BENCHMARK

* for those with ESHI
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + 810  - 1.045*assets - earnings if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==0 
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse +  810  - 1.045*assets - earnings - spouse_income if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==2
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse + 2071 - 1.045*assets - earnings - spouse_income  if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==1 & Spouse_status==3

* UNINSURED IN EXPERIMENT
* experiment with UHI - now have to include total expenditures rather than OOP
* NOTE WE DON'T WANT TO SUBTRACT THE UHI PREMIUMS BECAUSE WE WANT THESE PREMIUMS TO COUNT AS PART OF THE GOV REVENUE
* no ESHI
* singles
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat  - 1.045*assets - earnings if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==0
* married
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse  - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==2  // spouse does not work
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse + 810 - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==3  // spouse works


gen Unpaid_bills=0
replace Unpaid_bills=OOP if tr_pay_alt==3 & age<65

gen UHI=0
replace UHI=MC_orig *.6 - OOP if ((I_treat==1 & ESHI==0) | (type2==1 & ESHI==0 ) ) & experiment==14 & age<65


collapse (mean) Total_tax UHI_premium UHI govt_transfer  Unpaid_bills gov_expenditure, by(experiment)
* OPEN DATA 
* THESE ARE THE NUMBERS IN THE BOTTOM PANEL OF THE TABLE (transposed)

sort experiment
rename Total_tax Total_tax1
rename govt_transfer govt_transfer1
rename Unpaid_bills Unpaid_bills1
rename gov_expenditure gov_expenditure1
merge experiment using "Temp\Table_UHI.dta"
drop _merge	
sort experiment
save "Temp\Table_UHI.dta", replace	
}


* Fifth panel: Gov revenue and expenditures per HH
qui{
clear 
use "Counterfactuals Stata Files/Counterfactual16.dta"
replace experiment=0 if experiment==16
append using "Counterfactuals Stata Files/Counterfactual14.dta"
keep if education==1
* how much revenue from UHI premiums
gen UHI_premium=0
replace UHI_premium = 810  if age<65 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5)

gen expenditures = Expenditures_pay if (I_treat==1 & I_pay==1) | type2==1 
replace expenditures = Expenditures_not_pay if I_pay==0
sum expenditures
* expenditures = OOP + tax paid + premiums
* first subtract premiums
*has ESHI: benchmark and experiment the same
gen Income_tax= expenditures
replace Income_tax= expenditures - 810 if age<=64 &  (offer_accepted==3 | offer_accepted==5) &  Spouse_status!=3 // has eshi and spouse not covered or no spouse. 
replace Income_tax= expenditures - 2071 if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==3
* experiment
replace Income_tax= expenditures - 810 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status!=3
replace Income_tax= expenditures - 810*2 if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) &  Spouse_status==3
* now subtract the spouse's OOP
replace Income_tax= Income_tax-OOP_spouse if married==1
* now subtract own OOP - only for those who treat and pay. 
replace Income_tax= Income_tax - OOP if type2==1 | (I_treat==1 & I_pay==1) //these always pay

gen Cons_tax = consumption * .057
gen Total_tax = Income_tax + Cons_tax

* now let's fix the 65+
replace expenditures=0 if age>64
replace Income_tax=0 if age>64 
replace Cons_tax=0 if age>64
replace Total_tax=0 if age>64

replace expenditures = Expenditures_pay if age>64
gen Mcare_premium = 854  if age>64
replace Mcare_premium = Mcare_premium + 854 if age>64 & married==1 
replace Mcare_premium =0 if age<65

replace Income_tax=expenditures - Mcare_premium - OOP if age>64 & married==0
replace Income_tax=expenditures - Mcare_premium - OOP - OOP_spouse  if age>64 & married==1

replace Cons_tax = consumption * .057 if age>64
replace Total_tax = Income_tax + Cons_tax if age>64

replace SS_income =0 if age<65
gen total_SS_income = SS_income + spouse_income if age>64 & married==1
replace total_SS_income = SS_income if age>64 & married==0
replace total_SS_income =0 if age<65

gen Mcare_bills= 0 if age<65
replace Mcare_bills=OOP if age>64 & married==0
replace Mcare_bills=OOP + OOP_spouse if age>64 & married==1

* USE BUDGET CONSTRAINT TO INFER TOTAL GOVT EXPENDITURES FOR EACH PERSON. THIS WILL BE THE SUM OF EVERYTHING
* 65+ PEOPLE
* A' + C -RA + ME = SS - MEDICARE_PREM - INCOME_TAX - C(TAX) + ME (1-MEDICARE_COPAY) + TR
* EVERYTHING ON THE RIGHT HAND SIDE IS GOVT TRANSFERS AND REVENUE FROM THIS THIS PERSON
gen gov_expenditure= assets_prime + consumption + OOP*2 + OOP_spouse*2 - 1.045*assets if age>=65 // cons is after tax. OOP*2 is the total medical expendiure of the old. Medicare pays half by assumption. 

replace spouse_income=0 if spouse_income==.
replace I_treat=1 if type2==1
replace I_pay=1 if type2==1 & ESHI==1 
replace I_pay=1 if type2==1 & ESHI==0 & experiment==14 

* for those <65. 
* In the benchmark, we need to worry about ESHI and ESHI premiums
* add the ESHI premium to expenditures = 810
* ESHI premium for couples is 2071
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse - 1.045*assets - earnings - spouse_income if age<=64 & experiment==0 & (offer_accepted!=3 & offer_accepted!=5) // NO INSURANCE, BENCHMARK
* the following 3 lines are for those with ESHI, which are the same in benchmark and UHI experiment, so we run them for both. If anyone is also on Medicaid, that's OK because Medicaid would only pay the OOP part after ESHI paid.
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + 810  - 1.045*assets - earnings if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==0 
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse +  810  - 1.045*assets - earnings - spouse_income if age<=64 & (offer_accepted==3 | offer_accepted==5) &  Spouse_status==2
replace gov_expenditure= assets_prime + consumption + OOP*I_treat + OOP_spouse + 2071 - 1.045*assets - earnings - spouse_income  if age<=64  & (offer_accepted==3 | offer_accepted==5) & married==1 & Spouse_status==3

* UNINSURED IN EXPERIMENT
* experiment with UHI - now have to include total expenditures rather than OOP
* NOTE WE DON'T WANT TO SUBTRACT THE UHI PREMIUMS BECAUSE WE WANT THESE PREMIUMS TO COUNT AS PART OF THE GOV REVENUE
* no ESHI
* singles
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat  - 1.045*assets - earnings if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==0
* married
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse  - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==2  // spouse does not work
replace gov_expenditure= assets_prime + consumption + MC_orig*.6*I_treat + OOP_spouse + 810 - 1.045*assets - earnings - spouse_income if age<=64 & experiment==14 & (offer_accepted!=3 & offer_accepted!=5) & married==1 &  Spouse_status==3  // spouse works



* GENERATING THINGS FOR TABLE IN PAPER
* BOTTOM PANEL
* TAX REVENUE ; SS+medicare - medicare premiums 
gen SS_Medicare =Mcare_premium -Mcare_bills-total_SS_income


gen Unpaid_bills=0
replace Unpaid_bills=OOP if tr_pay_alt==3 & age<65
* in the benchmark, for those with ESHI, the unpaid part is only the OOP (ESHI always pays the rest). for those without ESHI, OOP is set to .6*MC. 
* in the experiment, it's the same for those with ESHI. If no ESHI, the unpaid part is just the OOP of the person. the rest gets paid by UHI

* UHI
gen UHI=0
replace UHI=MC_orig *.6 - OOP if ((I_treat==1 & ESHI==0) | (type2==1 & ESHI==0 ) ) & experiment==14 & age<65
gen UHI_total= UHI- UHI_premium

collapse (mean) Total_tax SS_Medicare govt_transfer UHI_total Unpaid_bills gov_expenditure, by(experiment)
* OPEN DATA 
* THESE ARE THE NUMBERS IN THE BOTTOM PANEL OF THE TABLE (transposed)
sort experiment
merge experiment using "Temp\Table_UHI.dta"	
drop _merge
sort experiment
save "Temp\Table_UHI.dta", replace	
}

clear
use "Temp\Table_UHI.dta"

order experiment col8  col3 col4 col5 col6  total  col7 I_treat I_shock I_Good max_age  wage_offer employed_yn yrs_work earnings_PV CV_earn family_consumption_PV CV_cons  TR Total_tax1 UHI_premium UHI govt_transfer1  Unpaid_bills1 gov_expenditure1  Total_tax SS_Medicare govt_transfer UHI_total Unpaid_bills gov_expenditure

xpose, clear
drop if v1==0 & v2==14
rename v1 Benchmark 
rename v2 Public_HI

* OPEN DATA -- COPY PASTE INTO TABLE 8 - NUMBERS IN COLUMNS ARE IN ORDER (ALL PANELS COMBINED)
}